package HDFS_Demo;
/*
如果出现权限问题报错可以用下面方法
1.修改环境变量 HADOOP_USER_NAME ROOT
2.修改hdfs配置 在hdfs-site.xml中
<property>
    <name>dfs.permissions</name>
    <value>false</value>
</property>
 */
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.apache.kerby.util.IOUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

//当前类为HDFS的连接类，实现HDFS的连接操作和HDFS的操作相关
public class Client {
    //conf 获得HDFS连接信息
    Configuration conf;
    FileSystem fs;

    @Before
    //创建方法：建立与HDFS连接
    public void init() throws IOException {
        //1 与HDFS的服务器进行连接
        conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://192.168.35.131:9000");
        fs = FileSystem.get(conf);
//        //JAVA传入参数，防止运行权限不足

        System.setProperty("HADOOP_USER_NAME","root");
        System.setProperty("HADOOP_ROOT+PASSWORD","1");

    }
    @Test
    //实现在HDFS上创建一个目录
    public void test() throws IOException {

        //2 在HDFS当中创建一个目录
//        FileSystem FS = FileSystem.mkdirs(new Path("/20240813-bd"));
        fs = FileSystem.get(conf);
        fs.mkdirs(new Path("/test2"));
        System.out.println("Create path success");
        //3 关闭连接
        fs.close();
    }
    //上传文档
    @Test
    public void putFile() throws IOException {
//        System.setProperty("HADOOP_USER_NAME","root");
//        System.setProperty("HADOOP_ROOT+PASSWORD","1");
        fs.copyFromLocalFile(true,true,new Path("D:\\test.txt"),
                new Path("/20240813-bd/test1/test.txt"));
        System.out.println("Put file success");
    }
    //删除文档
    @Test
    public void deleteFile() throws IOException {
        fs.delete(new Path("/20240813-bd/test1/test.txt"));
        System.out.println("Delete success");
    }
    //下载文档
    @Test
    public void downloadFile() throws IOException {
        fs.copyToLocalFile(true,new Path("/20240813-bd/test1/test.txt"),new Path("D:\\test.txt"));
        System.out.println("ToLocal success");
    }
    //查看文档
    @Test
    public void viewFile() throws IOException {
        RemoteIterator<LocatedFileStatus> status = fs.listFiles(new Path("/"),true);
        while (status.hasNext()){
            LocatedFileStatus fileStatus =status.next();
            //输出文件名称
            System.out.println("文件名为: "+fileStatus.getPath().getName());
            System.out.println("文件路径是: "+fileStatus.getPath());
            System.out.println("文件的权限为: "+fileStatus.getPermission());
            System.out.println("文件的大小为: "+fileStatus.getLen());
            System.out.println("文件的所属用户: "+fileStatus.getOwner()+" 文件的所属组: "+fileStatus.getGroup());
            System.out.println("=====================================");
        }
    }
    @Test
    public void orintFile() throws IOException {
        //定义一个Stream输入流,接受来自fs中读取到的文件内容
        InputStream in = fs.open(new Path("/20240813-bd/test1/test.txt"));
        //使用IOUtils类的copyBytes方法将输入流中的内容拷贝到System.out中，拷贝的缓冲区大小为512
        IOUtils.copyBytes(in,System.out,4096);
//        in.close();
        //关闭输入流
        IOUtils.closeStream(in);
        System.out.println();
        System.out.println("read success");
    }
    @After
    public void close() throws IOException {
        fs.close();
    }
}
